<template>
  <div>
    <div id="main"></div>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
export default {
  computed: {
    ...mapGetters({
      list: "cate/list",
    }),
  },
  methods: {
    ...mapActions({
      reqList: "cate/reqList",
    }),
  },

  mounted() {
    //发请求
    this.reqList();
  },
  watch: {
    // 监听list的改变，有数据了，就画图
    list: {
      handler() {
        if (this.list.length > 0) {
          console.log(this.list);
          // 基于准备好的dom，初始化echarts实例
          var myChart = require("echarts").init(
            document.getElementById("main")
          );

          // 指定图表的配置项和数据
          var option = {
            title: {
              text: "分类子类数量",
            },
            tooltip: {},
            legend: {
              data: ["子类数量"],
            },
            xAxis: {
              data:this.list.map(item=>item.catename),
            },
            yAxis: {},
            //数据
            series: [
              {
                name: "子类数量",
                type: "line",
                data: this.list.map(item=>item.children?item.children.length:0),
              },
            ],
          };

          // 使用刚指定的配置项和数据显示图表。
          myChart.setOption(option);
        }
      },
      deep: true,
    },
  },
};
</script>
<style scoped lang="less">
@import "../../less/index.less";
#main {
  width: 80%;
  margin: auto;
  height: 400px;
  border: 1px solid @primary;
}
</style>